package com.coder.bike.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.bike.entity.Violation;
import com.coder.bike.service.IViolationService;
import com.coder.bike.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 共享单车违规记录 前端控制器
 * </p>
 *
 * @author 沈诩宸
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/bike/violation")
@Api(tags = "共享单车违规记录")
@Slf4j
public class ViolationController {

    @Autowired
    private IViolationService violationService;

    /**
     * 分页查询违规记录
     * @param start 当前页
     * @param size 每页数量
     * @param violation 查询条件
     * @return 分页结果
     */
    @PostMapping("{start}/{size}")
    @ApiOperation("分页查询违规记录")
    public Result searchByPage(
            @PathVariable  Integer start,
            @PathVariable  Integer size,
           @RequestBody Violation violation) {
        Page<Violation> page = new Page<>(start, size);
        return Result.success(violationService.searchByPage(page, violation));
    }

    /**
     * 根据ID获取违规记录
     * @param id 违规记录ID
     * @return 违规记录详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取违规记录")
    public Result getById(@PathVariable("id") Long id) {
        Violation violation = violationService.getById(id);
        return violation != null ? Result.success().setData(violation) : Result.fail();


    }

    /**
     * 新增违规记录
     * @param violation 违规记录信息
     * @return 操作结果
     */
    @PostMapping
    @ApiOperation("新增违规记录")
    public Result save(@RequestBody Violation violation) {
        return violationService.save(violation) ? Result.success() : Result.fail();

    }

    /**
     * 更新违规记录
     * @param violation 违规记录信息
     * @return 操作结果
     */
    @PutMapping
    @ApiOperation("更新违规记录")
    public Result update(@RequestBody Violation violation) {
        return violationService.updateById(violation) ? Result.success() : Result.fail();
    }

    /**
     * 删除违规记录
     * @param ids 违规记录ID，多个ID用逗号分隔
     * @return 操作结果
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除违规记录")
    public Result delete(@PathVariable("ids") String ids) {
        return violationService.delete(ids) ? Result.success() : Result.fail();
    }
}